package com.kawaiior.asoulworld.world.ichunkgenerator;


import com.kawaiior.asoulworld.util.CommonDef;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.init.Biomes;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.ChunkPrimer;
import net.minecraft.world.gen.IChunkGenerator;

import java.util.List;
import java.util.Random;

public class ChunkGeneratorAsoul implements IChunkGenerator {

    private static final int heightLimit = 23;
    private final Random rand;
    private final World world;
    private final boolean generateStructures;

    public ChunkGeneratorAsoul(World world, boolean generate,long seed) {
        this.rand = new Random(seed);
        this.world = world;
        this.generateStructures = generate;
        world.setSeaLevel(63);
    }

    void setSeedFor(int x, int y, int z)
    {
        rand.setSeed((long)x * 341873128712L + (long)z * 132897987541L + (long)y * 438951276L);
    }

//    boolean chunkUsed(int x, int z)
//    {
//        return (x != 0 && z != 0);
//    }

    private int chunkLakeBuild(int x,int z)
    {
        if (x%1024==0)
        {
            if (z%1024==0)
            {
                return 1;
            }
            if ((z+16)%1024==0)
            {
                return 2;
            }
        }
        if ((x+16)%1024==0)
        {
            if (z%1024==0)
            {
                return 4;
            }
            if ((z+16)%1024==0)
            {
                return 3;
            }
        }
        return 0;
    }

    private boolean chunkLakeUsed(int x,int z)
    {
        return x%1024==0 || z%1024==0 || (x+16)%1024==0 || (z+16)%1024==0;
    }

    public void buildChunkLake(int x,int z,ChunkPrimer primer)
    {
        if (!chunkLakeUsed(x,z)){return;}
        switch (chunkLakeBuild(x, z))
        {
            case 0:break;
            case 1:
                for (int dx = 0;dx<3;dx++)
                {
                    for (int dz = 0;dz<3;dz++)
                    {
                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
                    }
                }
                break;
            case 2:
                for (int dx = 0;dx<3;dx++)
                {
                    for (int dz = 15;dz>12;dz--)
                    {
                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
                    }
                }break;
            case 3:
                for (int dx = 15;dx>12;dx--)
                {
                    for (int dz = 15;dz>12;dz--)
                    {
                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
                    }
                }
                break;
            case 4:
                for (int dx = 15;dx>12;dx--)
                {
                    for (int dz = 0;dz<3;dz++)
                    {
                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
                    }
                }
                break;
        }
    }

    private void buildChunk(int x, int z, ChunkPrimer primer)
    {
        for (int y = 0; y < heightLimit; y+= CommonDef.CHUNK_SIZE)
        {
            for (int dx = 0; dx < CommonDef.CHUNK_SIZE; dx++)
            {
                for (int dz = 0; dz < CommonDef.CHUNK_SIZE; dz++)
                {
                    for (int dy = 0; dy < CommonDef.CHUNK_SIZE; dy++)
                    {
                        if (y==0 && dy<=1)
                        {
                            primer.setBlockState(dx, y+dy, dz, Blocks.BEDROCK.getDefaultState());
                        }
                        else if (y<CommonDef.CHUNK_SIZE || (y==CommonDef.CHUNK_SIZE && dy <=12))
                        {
                            primer.setBlockState(dx, y+dy, dz, Blocks.STONE.getDefaultState());
                        }
                        else if (dy<=14)
                        {
                            primer.setBlockState(dx, y+dy, dz, Blocks.DIRT.getDefaultState());
                        }
                        else if (dy == 15)
                        {
                            primer.setBlockState(dx, y+dy, dz, Blocks.GRASS.getDefaultState());
                        }
                    }
                }
            }
        }

//        switch (chunkLakeBuild(x, z))
//        {
//            case 0:break;
//            case 1:
//                for (int dx = 0;dx<3;dx++)
//                {
//                    for (int dz = 0;dz<3;dz++)
//                    {
//                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
//                    }
//                }
//                return;
//            case 2:
//                for (int dx = 0;dx<3;dx++)
//                {
//                    for (int dz = 15;dz>12;dz--)
//                    {
//                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
//                    }
//                }return;
//            case 3:
//                for (int dx = 15;dx>12;dx--)
//                {
//                    for (int dz = 15;dz>12;dz--)
//                    {
//                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
//                    }
//                }
//                return;
//            case 4:
//                for (int dx = 15;dx>12;dx--)
//                {
//                    for (int dz = 0;dz<3;dz++)
//                    {
//                        primer.setBlockState(dx, 32, dz, Blocks.GOLD_BLOCK.getDefaultState());
//                    }
//                }
//        }
    }

    @Override
    public Chunk generateChunk(int x, int z)
    {
//        this.rand.setSeed((long)x * 672328094L + (long)z * 672346917L);
        ChunkPrimer chunkprimer = new ChunkPrimer();

        buildChunk(x,z,chunkprimer);


        Chunk chunk = new Chunk(this.world, chunkprimer, x, z);
        byte[] abyte = chunk.getBiomeArray();

        for (int i = 0; i < abyte.length; ++i)
        {
            abyte[i] = (byte)Biome.getIdForBiome(Biomes.EXTREME_HILLS);
            //abyte[i] = (byte)Biome.getIdForBiome(InitBiome.BIOME_ONE);
        }

        chunk.resetRelightChecks();
        return chunk;
    }

    @Override
    public void populate(int x, int z) {

    }

    @Override
    public boolean generateStructures(Chunk chunkIn, int x, int z) {
        return false;
    }

    @Override
    public List<Biome.SpawnListEntry> getPossibleCreatures(EnumCreatureType creatureType, BlockPos pos) {
        return null;
    }

    @Override
    public BlockPos getNearestStructurePos(World worldIn, String structureName, BlockPos position, boolean findUnexplored) {
        return null;
    }

    @Override
    public void recreateStructures(Chunk chunkIn, int x, int z) {

    }

    @Override
    public boolean isInsideStructure(World worldIn, String structureName, BlockPos pos) {
        return false;
    }
}
